/* 用来控制各房间、各爬虫的数量的api集合 */
import {generateID} from '@/utils'
export default {
    // 设置爬虫数量api
    setnum:{
        help():string{
            return "该模块是设置爬虫数量的模块！"
        },
        // 设置房间的memory数据
        set(roomName:string,creepRole:string,num:number):string{
            var apiRoom = Game.rooms[roomName]
            // 如果存在该房间，且该房间是我的
            if (apiRoom && apiRoom.controller.my)
            {
                if (!apiRoom.memory.creepNumData) apiRoom.memory.creepNumData = []
                // 接下来检查creepNumData中是否有该role的数据，有则直接修改，没有则新建
                if (apiRoom.memory.creepNumData.length > 0)
                for (var i of apiRoom.memory.creepNumData)
                {
                    if (i.creeprole == creepRole)
                    {
                        i.num = num
                        return `[setnum]爬虫${creepRole}的数量已经修改为${num}`
                    }
                }
                apiRoom.memory.creepNumData.push({creeprole:creepRole,num:num})
                return `[setnum]爬虫${creepRole}的数量已经设置为${num}`
            }
            return `[setnum]出现错误，请检查该房间是否存在或属于你`
        },
        // 清空该房间的所有roomNumData
        clean(roomName:string):string{
        var apiRoom = Game.rooms[roomName]
        // 如果存在该房间，且该房间是我的
        if (apiRoom && apiRoom.controller.my)
        {
            apiRoom.memory.creepNumData = []
            return `[setnum]房间${roomName}的creepNumData已清空`
        }
        return `[setnum]出现错误，请检查该房间是否存在或属于你`
        },
        // 移除该房间指定rolename的数据
        remove(roomName:string,creepRole:string):string{
            var apiRoom = Game.rooms[roomName]
            // 如果存在该房间，且该房间是我的
            if (apiRoom && apiRoom.controller.my)
            {
                if (!apiRoom.memory.creepNumData) apiRoom.memory.creepNumData = []
                if (apiRoom.memory.creepNumData.length > 0)
                for (var i of apiRoom.memory.creepNumData)
                {
                    if (i.creeprole == creepRole)
                    {
                        apiRoom.memory.creepNumData = _.difference(apiRoom.memory.creepNumData,[i])
                        return `[setnum]房间${roomName}中creepNumData中${creepRole}已清空`
                    }
                }
                return `[setnum]房间${roomName}未发现${creepRole}数据`
            }
            return `[setnum]出现错误，请检查该房间是否存在或属于你`
        },
        show(roomName:string):string
        {
            var apiRoom = Game.rooms[roomName]
            // 如果存在该房间，且该房间是我的
            if (apiRoom && apiRoom.controller.my)
            {
                var result:string = `[setnum] Room:${roomName},result:\n`
                if (!apiRoom.memory.creepNumData) apiRoom.memory.creepNumData = []
                if (apiRoom.memory.creepNumData.length > 0)
                {
                    for (var i of apiRoom.memory.creepNumData)
                    {
                        result += `RoleName: ${i.creeprole} Number: ${i.num}\n`
                    }
                    return result
                }
                else return `[setnum]房间${roomName}未发现数据`
            }
            return `[setnum]出现错误，请检查该房间是否存在或属于你`
        },
        showall(roomName:string):string{
            if (!global.DicCreepFrameControl[roomName])
            return `[setnum]房间${roomName}未在全局挂载【框架控制模块】`
            else
            {
                if (Object.keys(global.DicCreepFrameControl[roomName]).length > 0)
                {
                    var k = global.DicCreepFrameControl[roomName]
                    let result = `[setnum]房间${roomName}\n`
                    for (var i of Object.keys(k)) result += `角色 ${k[i].work} 数量 ${k[i].num} 框架方法:${k[i].fun?k[i].fun.name:"非框架"}\n`
                    return result
                }
                return `[setnum]房间${roomName}的全局数量控制模块长度为0`
            }
        }
    },
    // 生成一个随机ID
    generateID():string{
        var a = Game.cpu.getUsed()
        var re = generateID()
        var rr = generateID()
        var b = Game.cpu.getUsed()
        console.log(b-a)
        console.log(re)
        console.log(rr)
        return 'over'
    },
    // 爬虫睡眠 (娱乐)
    zzZ(roomName:string):string{
        var thisRoom = Game.rooms[roomName]
        if (!thisRoom) return `error`
        if (thisRoom.memory.zzZ)
        thisRoom.memory.zzZ = false
        else
        thisRoom.memory.zzZ = true
        return `zzZ:${thisRoom.memory.zzZ}`
    },
    
}